<?php
Class Pager{

	public static function Create($count, $url){

	$request = getRequest::getInstance();
	(int)$page = $request->SetLimit(3)->SetDefault(1)->getVar('page', 'GET');

        if(isset($_COOKIE['per_page'])){
            $nums = $_COOKIE['per_page'];
        }else{
            $nums = $_SESSION['config']['page']['per_page'];
        }
        

	$pages = ceil($count/$nums);

	if($page < 1){
		$page = 1;
	}
	elseif($page > $pages){
		$page = $pages;
	}

	$start = ($page-1)*$nums;
	// когда у нас в таблице нет записей
	if ($start < 0)$start = 0;
        
	$limit = $start.','.$nums;

        $neighbours = 6;
        $left_neighbour = $page - $neighbours;
        if ($left_neighbour < 1) $left_neighbour = 1;
        $right_neighbour = $page + $neighbours;
        if ($right_neighbour > $pages) $right_neighbour = $pages;

        $pager = NULL;
        if($pages>1){
            if($page > 1){
                    $pager .= "<a href=\"".BASE_URL.$url.$left_neighbour."\">Початок</a> <a href=\"".BASE_URL.$url.($page-1)."\" class=\"page larger\">←</a>";
            }
			for ($i=$left_neighbour; $i<=$right_neighbour; $i++) {
				if ($i != $page) {
					$pager .= "<a href=\"".BASE_URL.$url.$i."\" class=\"page larger\">".$i.'</a>';
				}else{
					// выбранная страница
					$pager .= '<span class="current">'.$i.'</span>';
				}
			}
            if ($page < $pages) {
            $pager .= " <a href=\"".BASE_URL.$url.($page+1)."\" class=\"page larger\">→</a> <a href=\"".BASE_URL.$url.$pages."\" class=\"nextpostslink\">Кінець</a>";
            }
		}
            return array('limit'=>$limit,'pager'=>$pager);
	}
}
?>
